const app = getApp()
var baseurl = app.baseurl.url;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    yuming: app.yuming.url,
    winWidth: 0,
    winHeight: 0,
    currentTab: 0,
    list: [
      {
        id: 1,
        price: 0,
        thumb: "/storage/uploads/20210831/00c5bf8b9343c500a181c5f29ef6d9d3.jpg",
        title: "波兰求购酱汁包装机设备"
      },
      {
        id: 2,
        name: '【必优特】营养鲜鸡蛋30枚快递装',
        phone: '￥89.90',
        sex: '../../img/dan.png',
        isTouchMove: false,
      },
      {
        id: 3,
        name: '【必优特】营养鲜鸡蛋30枚快递装',
        phone: '￥89.90',
        sex: '../../img/dan.png',
        isTouchMove: false,
      },
      {
        id: 4,
        name: '【必优特】营养鲜鸡蛋30枚快递装',
        phone: '￥89.90',
        sex: '../../img/dan.png',
        isTouchMove: false,
      },
      {
        id: 5,
        name: '【必优特】营养鲜鸡蛋30枚快递装',
        phone: '￥89.90',
        sex: '../../img/dan.png',
        isTouchMove: false,
      },
      {
        id: 6,
        name: '【必优特】营养鲜鸡蛋30枚快递装',
        phone: '￥89.90',
        sex: '../../img/dan.png',
        isTouchMove: false,
      },
      {
        id: 7,
        name: '【必优特】营养鲜鸡蛋30枚快递装',
        phone: '￥89.90',
        sex: '../../img/dan.png',
        isTouchMove: false,
      },
    ],
    total: '', // 总共条数
    per_page: 4, // 每页条数
    last_page: 1, // 最后一页
    current_page: 1,// 当前页
  },
  onLoad: function (options) {
    var that = this;

    /**
     * 获取当前设备的宽高
     */
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }

    });
    this.list()
  },

  // 列表
  list() {
    wx.request({
      url: `${baseurl}/user.UserCollect/collect_list`,
      method: 'POST',
      data: {
        type: 2, // 1为商品、2为兑换
        user_id: wx.getStorageSync("user_id"),
        openid: wx.getStorageSync("openid"),
      },
      success: (res) => {
        this.setData({
          list: res.data.data.data,
          current_page: res.data.data.current_page,
          last_page: res.data.data.last_page,
          per_page: res.data.data.per_page,
          total: res.data.data.total
        })
      }
    })
  },
  // 删除
  close() {
    wx.request({
      url: `${baseurl}/user.UserCollect/collect_del`,
      method: 'POST',
      data: {
        user_id: wx.getStorageSync("user_id"),
        id: this.data.id,
      },
      success: (res) => {
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
          duration: 2000
        })
        this.list()
      }
    })
  },

  touchstart: function (e) {
    //开始触摸时 重置所有删除
    let data = app.touch._touchstart(e, this.data.list) //将修改过的list setData
    this.setData({
      list: data,
      id: e.currentTarget.dataset.id // 删除这个id的收藏
    })
  },
  //滑动事件处理
  touchmove: function (e) {
    let data = app.touch._touchmove(e, this.data.list, 'id')//将修改过的list setData

    this.setData({
      list: data
    })
  },
  click() {
    wx.redirectTo({
      url: '/pages/collet/collet',
    })
  },
  /**
 * 页面上拉触底事件的处理函数
 */
  // 触底加载
  onReachBottom: function () {
    var that = this;
    this.getMore1(that)
  },
  // 触底加载
  getMore1(that) {
    that.setData({
      isloading: true
    })
    var page = that.data.current_page;//页码
    // console.log(that.data.current_page);
    if (that.data.last_page == page) {
      wx.showToast({
        title: '没有更多数据！',
        icon: 'none',
        duration: 2000
      })
    } else {
      page++;
      this.setData({
        current_page: page
      })
      // 需求列表
      wx.request({
        url: `${baseurl}/user.UserCollect/collect_list`,
        method: 'POST',
        data: {
          title: that.data.title,
          order: that.data.order,
          desc: that.data.desc,
          total: '', // 总共条数
          per_page: 4, // 每页条数
          last_page: 1, // 最后一页
          current_page: 1,// 当前页
        },
        success: (res) => {
          this.setData({
            list: [...this.data.list, ...res.data.data.data],
            current_page: res.data.data.current_page,
            last_page: res.data.data.last_page,
            per_page: res.data.data.per_page,
            total: res.data.data.total
          })
        }
      })
    }

  },

  /**
 * 生命周期函数--监听页面显示
 */
  onShow: function () {
    this.onLoad()
  },

})